# EventHandler code生成器

def upperHeader(word):
    return word[0].upper() + word[1:]

def lowerHeader(word):
    return word[0].lower() + word[1:]

def write_file_append(filename, content):
    with open(filename, 'a') as f:
        f.write(content)

def write_file_append_bytes(filename, content):
    with open(filename, 'ab') as f:
        f.write(content)

action_name = 'afterClosed'
actionname_headup = upperHeader(action_name)
actionname_headdown = lowerHeader(action_name)
u = actionname_headup
d = actionname_headdown

interface_javafile_name = '%sEventHandler.java'%(u)

interface_code = ('''
// 呵呵
@FunctionalInterface
public interface %sEventHandler {
    void %s(Object sender, IPort port);
}

'''%(u,d))

impl_code = ('''
    private List<%sEventHandler> %sEventHandlers = new ArrayList<>();

    public void add%sEventHandler(%sEventHandler event) {
        %sEventHandlers.add(event);
    }

    public void remove%sEventHandler(%sEventHandler event) {
        %sEventHandlers.remove(event);
    }

    public void callEvent%s(Object sender, IPort port) {
        for (%sEventHandler %sEventHandler : %sEventHandlers) {
            %sEventHandler.%s(sender, port);
        }
    }
'''%(u,d,u,u,d, u,u,d, u,u,d,d,d,d))

print(interface_javafile_name)
print(interface_code)
print(impl_code)

# write_file_append(interface_javafile_name, interface_code)                        # 编码取决于操作系统
# write_file_append_bytes(interface_javafile_name, interface_code.encode('utf8'))   # UTF8